package com.community.community.service;

import com.community.community.BaseContent;
import com.community.community.mapper.UserMapper;
import com.community.community.pojo.Result;
import com.community.community.pojo.dto.UserDto;
import com.community.community.pojo.entity.User;
import com.community.community.untils.JwtUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    public String register(UserDto userDto) {
        User exist = userMapper.selectByUserName(userDto.getUserName());
        if(exist!=null){
            return BaseContent.USERNAME_EXIST;
        }
        User user = new User();
        user.setUserName(userDto.getUserName());
        user.setPassword(passwordEncoder.encode(userDto.getPassword()));
        userMapper.insert(user);
        return BaseContent.REGISTER_SUCCESS;
    }

    public Result login(UserDto userDto) {
        User exist = userMapper.selectByUserName(userDto.getUserName());
        if(exist.getId()==null){
            return Result.fail(BaseContent.USERNAME_NOT_EXIST);
        }else if(passwordEncoder.matches(userDto.getPassword(),exist.getPassword())) {
            String token = jwtUtil.generateToken(userDto.getUserName(),exist.getId());
            return Result.success((Object) token);
        }
        return Result.fail(BaseContent.WRONG_PASSOWRD);
    }

    public Result update(UserDto userDto, HttpServletRequest request) {
        Integer user_id = jwtUtil.extractUserId(request);
        if(userDto.getPassword()!=null){
            boolean change = changePassword(user_id, userDto.getOldPassword(), userDto.getPassword());
            if(change) {
                return Result.success();
            }
            return Result.fail("密码更改失败");

        }
        User user = new User();
        user.setId(user_id);
        user.setPetName(userDto.getPetName());
        user.setEmail(userDto.getEmail());
        user.setHeadImage(userDto.getHeadImage());
        userMapper.updateById(user);
        return Result.success();
    }

    private boolean changePassword(Integer user_id, String oldPassword,String newPassword){
        String password = userMapper.getPasswordById(user_id);
        if(passwordEncoder.encode(oldPassword).equals(password)){
            User user = new User();
            user.setPassword(passwordEncoder.encode(newPassword));
            user.setId(user_id);
            userMapper.updateById(user);
            return true;
        }
        return false;
    }

    public Result getInfo(HttpServletRequest request) {
        String username = jwtUtil.extractUsername(request);
        User user = userMapper.selectByUserName(username);
        return Result.success(user);
    }
}
